var commentLen = 200 ;

$(function(){
	$send = $('#contactForm .send');
	$('#contactForm').find('li.send a').click(function(){ $('#contactForm').submit();  return false;});
	strLimit('#msg','#msgNum',commentLen,1);
	/*
	$('#msg').focus(function(){
		CreatEditor("msg" ,'100%' );		
	});
	*/
	$('#commentsForm').submit(function(){ commentsUpload(this);return false; });
	
	delComment('#talk div.operate a.del');
	
});
function commentsCheck(obj){

	if( '' == $('#msg').val() ) return false; //$('#commentsForm .info').html();	
	return true;
}

function commentsUpload(obj){
	if( !isLogin() && commentType != 'judge' ){
		openLogin(obj);
		return false;
	};
	
	setTimeout(function(){
		if( '' == $('#msg').val() ){
			$.openWin({'msg':'^_^.你还没写评论呢.','status':'error','time':1500});
			return false;
		}
		commentSubmit(obj);
	},600);
	return false;
};

function commentSubmit(obj){
	
	var $obj = $(obj);
	$(obj).find('input[type=submit]').attr('disabled',true).after(loading);
	$('#msg').attr('disabled',true);
	post() ;	
	function post(){
		if(!commentsCheck(obj))  return false;
		var sendData = {'msg': $('#msg').val(), 'parent_id': $('#comment_id').val(), 'task_id': $('#task_id').val(), 'user_name': $('#user_name').val() ,'commentType':commentType,'dataType':'ajax'};
		$.post( $obj.attr('action'),sendData,function(data){
			statusCheck(data,obj);
			var $show = $('#talk .list').prepend(data.msg).find('li:first').slideDown('slow');

			if( $show.hasClass('noExist')){
				setTimeout(function(){$show.slideUp('normal',function(){$(this).remove();});},3000);
			}else{
				//FCK_Empty('msg');
				$.openWin({'msg':'','time':1000, 'bg':false});
				//$('#msg').val('');
				//strLimit('#msg','#msgNum',commentLen,1);
				setTimeout(function(){$('#talk .beautyImg').corner("6px") ;},20);
			}
			$obj.find('input[type=submit]').attr('disabled',false).next('.loading').remove();
			$('#msg').attr('disabled',false);
		},'json');
	};	
};

function delComment(obj){
	$(obj).live('click',function(){
		var $a = $(this);
		$.post($(this).attr('href'),function(data){
			if(data.status == 'ok'){
				$.openWin({'msg':data.msg,'time':1500, 'bg':false});
				$a.parent().parent().parent()
					.slideUp('normal',function(){
						$(this).remove();
						if( $('#talk .commentlist > li').length == 0 )
							window.location.reload();
					}).next('hr').remove();
			}else{
				alert(data.msg);
			}
		},'json');
		return false;
	});
};